package com.gxa.service.impl.system;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gxa.component.cache.LocalCache;
import com.gxa.mapper.system.MenuMapper;
import com.gxa.pojo.entity.system.Menu;
import com.gxa.service.system.MenuService;
import com.gxa.utils.AddressTreeUtils;
import com.gxa.utils.QueryResult;
import com.gxa.utils.UserThreadLocal;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author lch
 * @Date 2024/2/22 15:52
 * @Description 员工管理
 **/
@Service
public class MenuServiceImpl implements MenuService {

    @Resource
    private MenuMapper menuMapper;

    @Resource
    private LocalCache localCache;

    @Override
    public int addMenu(Menu menu) {
        if (menu.getTitle() == null || menu.getTitle().equals("")) {
            return -1;
        }
        return menuMapper.insert(menu);
    }

    @Override
    public Integer updateMenu(Menu menu) {
        if (!(menu.getTitle() == null && menu.getTitle().equals(""))) {
            return -1;
        }
        return menuMapper.update(menu);
    }

    @Override
    public Integer deleteMenuById(Integer id) {
        return menuMapper.delete(id);
    }

    @Override
    public QueryResult queryAllMenu(Menu menu) {
        //分页查询
        Page<Menu> page = PageHelper.startPage(menu.getPage(), menu.getLimit());

        List<Menu> menuList = menuMapper.query(menu);

        QueryResult queryResult = new QueryResult();
        queryResult.setData(menuList);
        queryResult.setCount(page.getTotal());
        return queryResult;
    }

    @Override
    public List<Menu> queryDynamicMenu(Integer userId) {

        if(userId==null){
            return LoginServiceImpl.menuList;
        }



       List<Menu> menuList = AddressTreeUtils.buildTree(menuMapper.queryDynamicMenu(userId));

        return menuList;
    }

}
